package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Employee;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
   private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
   private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        Long id=dish.getId();
        List<DishFlavor> flavor=dishDTO.getFlavors();
        if (flavor!=null&&flavor.size()>0){
            flavor.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            dishFlavorMapper.insertBatch(flavor);
        }
    }
    @Transactional
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page= dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Transactional
    public void delete(List<Long> ids) {
        ids.forEach(id->{
            Dish dish=dishMapper.getId(id);
            if (dish.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        List<Long> setmealIds =setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealIds !=null && setmealIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);

        }
        ids.forEach(id->{
            dishMapper.delete(id);
            dishFlavorMapper.delete(id);
        });
    }

    @Override
    public Object getById(Long id) {
        Dish dish=dishMapper.getId(id);
        List<DishFlavor> list=dishFlavorMapper.getId(id);
        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(list);
        return dishVO;
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        dishFlavorMapper.delete(dishDTO.getId());
        List<DishFlavor> list=dishDTO.getFlavors();
        if (list != null && list.size()>0){
            list.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
        }
        dishFlavorMapper.insertBatch(list);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Dish updateDish=new Dish();
        updateDish.setId(id);
        updateDish.setStatus(status);
        dishMapper.update(updateDish);

        if (status==StatusConstant.DISABLE){
            List<Long> list=new ArrayList<>();
           list.add(id);
            List<Long> setmealIdList=setmealDishMapper.getSetmealIdsByDishIds(list);
            if (setmealIdList != null&&setmealIdList.size()!=0){
                for (Long setmealId : setmealIdList) {
                    Setmeal setmeal=new Setmeal();
                    setmeal.setStatus(StatusConstant.DISABLE);
                    setmeal.setId(setmealId);
                    setmealMapper.update(setmeal);
                }
            }
        }
    }
    //查询分类ID中的菜品
    @Override
    public List list(Long categoryId) {
        Dish dish=Dish.builder().categoryId(categoryId).
                status(StatusConstant.ENABLE).
                build();
        List list = dishMapper.list(dish);
        return list;
    }
}

